//
//  MainViewController.swift
//  SwiftDemo
//
//  Created by 李文强 on 2017/1/10.
//  Copyright © 2017年 李文强. All rights reserved.
//

import UIKit

class MainViewController: UIViewController,PunchLittleViewDelegate,UITableViewDelegate,UITableViewDataSource,SearchBarClickDelegate{

    override func viewDidLoad() {
        super.viewDidLoad()
//        self.navigationController?.navigationBar.topItem?.title = "明星"
        //隐藏系统的导航栏 不然点击事件受到影响
        self.navigationController?.isNavigationBarHidden=true
        self.test()
        self.createTableView()
        // Do any additional setup after loading the view.
    }
    //MARK: 导航栏上方变成按钮（swift的标记方法）
    var isClick:Bool = false
    var titleLabel = UILabel()
    func test() {
        // 创建一个导航栏
        let navBar = UINavigationBar(frame: CGRect(x:0, y:0, width:self.view.frame.size.width, height:64))
        // 导航栏背景颜色
        navBar.backgroundColor = UIColor.white
        //这里是导航栏透明
        //navBar.shadowImage = UIImage()
        //navBar.setBackgroundImage(UIImage(), for: UIBarMetrics.default)
        
        // 自定义导航栏的title，用UILabel实现
        titleLabel.frame = CGRect(x:0,y:0,width:50,height:60)
        titleLabel.text = "明星"
        titleLabel.textColor = UIColor.darkGray
        titleLabel.isUserInteractionEnabled = true
        let tap = UITapGestureRecognizer.init(target: self, action: #selector(clickAction(tap:)))
        titleLabel.addGestureRecognizer(tap)
        
        //这里使用系统自定义的字体
        //titleLabel.font = UIFont(name: "Roboto-Medium", size: 16)
        
        // 创建导航栏组件
        let navItem = UINavigationItem()
        // 设置自定义的title
        navItem.titleView = titleLabel
        
        // 创建左侧按钮
        let leftButton = UIBarButtonItem(title: "Fond", style: .plain, target: self, action: nil)
        leftButton.tintColor = UIColor.darkGray
        
        // 创建右侧按钮
        let rightButton = UIBarButtonItem(title: "Private", style: .plain, target: self, action: nil)
        rightButton.tintColor = UIColor.darkGray
        
        // 添加左侧、右侧按钮
        navItem.setLeftBarButton(leftButton, animated: false)
        navItem.setRightBarButton(rightButton, animated: false)
        
        navigationItem.setHidesBackButton(true, animated: false)
        // 把导航栏组件加入导航栏
        navBar.pushItem(navItem, animated: false)
        
        // 导航栏添加到view上
        self.view.addSubview(navBar)
    }
    let backView:UIView = UIView.init()
    let punchView = PunchLittleView()
    //MARK:点击出现弹出的View
    func clickAction(tap:UITapGestureRecognizer) -> Void {
        backView.frame = self.view.frame
        backView.backgroundColor = UIColor.clear
        backView.alpha = 0.4
        let backTap = UITapGestureRecognizer.init(target: self, action:#selector(disMissView(tap:)))
        backView.addGestureRecognizer(backTap)
        
        punchView.delegate = self
        punchView.frame = CGRect(x:Screen.width/2-60,y:64,width:120,height:200)
        punchView .creatView()
        punchView.alpha = 0
        punchView.backgroundColor = UIColor.darkGray
        self.view .addSubview(backView)
        self.view .addSubview(punchView)
        UIView.animate(withDuration: 0.5, animations:{
            self.punchView.alpha = 0.9
        })
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //MARK:点击View，弹出框消失
    func disMissView(tap:UITapGestureRecognizer) -> Void {
        UIView.animate(withDuration: 0.5, animations: {
            self.punchView.alpha = 0
            self.backView.removeFromSuperview()
        })
      
    }
    
    func getPuchName(name: String) {
        titleLabel.text = name
        UIView.animate(withDuration: 0.5, animations: {
            self.punchView.alpha = 0
            self.backView.removeFromSuperview()
        })
        //MARK:refresh controller
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    //MARK: - tableView
    let searv = SearchView()
    
    lazy var tableView:UITableView = {
        let tableV = UITableView.init(frame: CGRect(x:0,y:64,width:Screen.width,height:Screen.height-64-49), style: UITableViewStyle.plain)
        tableV.backgroundColor = UIColor.white
        tableV.showsVerticalScrollIndicator = false
        tableV.showsHorizontalScrollIndicator = false
        
        self.searv.delegate = self
        self.searv.frame = CGRect(x:0,y:0,width:Screen.width,height:40)
        self.searv.createSearchBar()
        tableV.tableHeaderView = self.searv
        tableV.tableHeaderView?.backgroundColor = UIColor.white
        return tableV
        }()
    func createTableView() -> Void {
        self.tableView.dataSource = self
        self.tableView.dataSource = self
        self.view.addSubview(self.tableView)
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
        
    }
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        return 100
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: "Cell")
        cell.backgroundColor = UIColor.white
        return cell
        
    }
    //MARK:SearchBarDelegate
    let searchVC = SearchViewController()
    
    func searchBarHasClick() {
        searchVC.view.frame = self.view.bounds
        searchVC.view.backgroundColor = UIColor.white
        self.view.addSubview(self.searchVC.view)
        
    }
    func searchDidEndClick() {
      searchVC.view.removeFromSuperview()
    }

}
